package com.yqg.education.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.education.common.Result;
import com.yqg.education.entity.Attachment;
import com.yqg.education.entity.Stage;
import com.yqg.education.entity.StuCertificate;
import com.yqg.education.mapper.StuCertificateMapper;
import com.yqg.education.service.IAttachmentService;
import com.yqg.education.service.IStuCertificateService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * StuCertificateController 前端控制器
 * @author 张芸博
 * @since 2025-04-10
 */
@RestController
@RequestMapping("/stu-certificate")
public class StuCertificateController {
    @Resource
    private StuCertificateMapper stuCertificateMapper;
    @Resource
    private IStuCertificateService stuCertificateService;

    @Resource
    private IAttachmentService attachmentService;

    /**
    *  新增或者更新
    */
    @PostMapping("/edit")
    public Result save(@RequestBody StuCertificate stuCertificate) {
        stuCertificate.setCreateTime(LocalDateTime.now());
        stuCertificateService.saveOrUpdate(stuCertificate);
        List<Attachment> attachmentList = new ArrayList<>();
        stuCertificate.getAttachment().forEach(att -> {
            if (att.getId() == null) {
                Attachment attachment = new Attachment();
                attachment.setCorrelationId(stuCertificate.getId());
                attachment.setCorrelationTable("stu_certificate");
                attachment.setFileName(att.getFileName());
                attachment.setFilePath(att.getFilePath());
                attachmentList.add(attachment);
            }
        });
        if (!attachmentList.isEmpty()) {
            attachmentService.saveOrUpdateBatch(attachmentList);
        }
        return Result.success();
    }
    /**
    *  删除
    */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        StuCertificate stuCertificate =stuCertificateService.getById(id);
        stuCertificate.setIsDelete(1);
        return Result.success(stuCertificateService.saveOrUpdate(stuCertificate));
    }
    /**
    *  查询所有
    */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<StuCertificate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
       List<StuCertificate> list=  stuCertificateService.list(queryWrapper);
        return Result.success(list);
    }
    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(stuCertificateService.getById(id));
    }
    /**
    *  分页查询
    */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                            Integer courseId,
                           @RequestParam(required = false) Integer classId) {
        pageNum = pageNum * pageSize - pageSize;
        List<StuCertificate> courseList = stuCertificateMapper.pageList(pageNum, pageSize, courseId, classId);
        Long count = stuCertificateMapper.findCount(courseId, classId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("records", courseList);
        map.put("total", count);
        return Result.success(map);
    }
}

